/*
 * @lc app=leetcode.cn id=874 lang=javascript
 *
 * [874] 模拟行走机器人
 */

// @lc code=start
/**
 * @param {number[]} commands
 * @param {number[][]} obstacles
 * @return {number}
 */
var robotSim = function(commands, obstacles) {
    let D = [[0,1],[1,0],[0,-1],[-1,0]], has = new Set(), ans = x = y = d = 0
    for(let o of obstacles){
        has.add(`${o[0]}_${o[1]}`)
    }
    for(let c of commands){
       if(c >=0){
           for(let i = 0 ; i < c  ;i++){
              let  next_x, next_y
              next_x = x + D[d][0] , next_y = y + D[d][1]
              if(has.has(`${next_x}_${next_y}`)) break
              x = next_x ; y = next_y;
              ans = Math.max(ans,x*x+y*y)
           }
       }else{
          d = c == -1 ? (d+1)%4 : (d+3)%4
       }

    }
    return ans

};
// @lc code=end

